package cn.com.homeyoda.aimate.aihealth.utils;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;


public class LoadingStateManager {
    private static LoadingStateManager instance;
    private final MutableLiveData<Boolean> isLoading = new MutableLiveData<>();
    private int loadingCount = 0;


    public static LoadingStateManager getInstance() {
        if (instance == null) {
            instance = new LoadingStateManager();
        }
        return instance;
    }

    private LoadingStateManager() {
        isLoading.setValue(false);
    }

    public LiveData<Boolean> getLoadingState() {
        return isLoading;
    }

    public void showLoading() {
//        if (loadingCount == 0) {
//            isLoading.postValue(true); // 仅在第一次调用时更新状态
//        }
//        loadingCount++;
        loadingCount++;
        isLoading.postValue(true);
    }

    public void hideLoading() {
        if (loadingCount > 0) {
            loadingCount--;
        }
        if (loadingCount == 0) {
            isLoading.postValue(false);
        }
    }

    public void reset() {
        loadingCount = 0;
        isLoading.postValue(false);
    }
}
